# **************************************
# --*-- coding: utf-8 --*--
# @Time    : 2024-11-05
# @Author  : white
# @FileName: reboot.py
# @Software: PyCharm
# **************************************
from tkinter import Button, \
    Variable, DISABLED, Radiobutton
from .base import Base
from comon import logger
from os import system


class Computer(Base):
    def __init__(self, ui):
        super().__init__()
        self.ui = ui
        self.left_frame = self.ui.left_frame
        self.right_frame = self.ui.right_frame
        self.name = "电脑操作"

        self.ui_init()

    def reg(self, option: str):
        if option == "关机":
            system("shutdown /s /t 0")
        elif option == "重启":
            system("shutdown /r /t 0")

    def ui_init(self):
        self._button()
        self.button.bind("<Button-1>", self.option)

    def _button(self):
        self.button = Button(self.left_frame, text=self.name)
        self.button.pack(pady=self.ui.pady_upper, anchor="w")

    @Base.clear_frame
    def option(self, event=None):
        if self.button['state'] == DISABLED:
            return
        self.option_text = Variable()

        self.shutdown = Button(self.right_frame, text="关机",
                               command=lambda: self.submit(_type=self.shutdown.cget("text")))
        self.shutdown.grid(row=1, column=0, sticky="w", pady=self.ui.pady_upper)

        self.reboot = Button(self.right_frame, text="重启",
                             command=lambda: self.submit(_type=self.reboot.cget("text")))
        self.reboot.grid(row=2, column=0, sticky="w", pady=self.ui.pady_upper)

    def submit(self, event=None, _type=None):
        try:
            self.reg(_type)
        except Exception as e:
            logger.error(e)
